package com.vf.cloud.scene.mvc.biz.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.vf.cloud.common.datasource.annotation.Tx;
import com.vf.cloud.common.util.R;
import com.vf.cloud.common.util.UuidUtil;
import com.vf.cloud.pass.common.repository.base.Thumbnail;
import com.vf.cloud.pass.mvc.biz.service.IThumbnail;
import com.vf.cloud.scene.common.domain.SubStyleTemplatePoi;

@RestController
@RequestMapping("/scene/biz/poi/template")
public class PoiTemplateController {
	
	@Autowired
	private IThumbnail thumbnailImpl;
	
	@GetMapping(value = { "/list" }, produces = "application/json; charset=utf-8")
	public R<Page<SubStyleTemplatePoi>> findList(HttpServletRequest request,
			@RequestParam(name = "projectId", required = false, defaultValue = "1") String projectId,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {

		String sqlExceptSelect = " FROM " + SubStyleTemplatePoi.TABLE_NAME + " O  WHERE 1=1 ";
		if(!StrKit.isBlank(projectId)) {
			sqlExceptSelect += " AND O.PROJECT_ID='"+projectId+"' ";
		}
		Page<SubStyleTemplatePoi> pages = SubStyleTemplatePoi.dao.paginate(cur, limit, "SELECT * ", sqlExceptSelect);
		return R.ok(pages);
	}
	
	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<SubStyleTemplatePoi> save(MultipartFile file1,MultipartFile file2, SubStyleTemplatePoi model) {
		try {
			Thumbnail normalEntity = null;
			Thumbnail activateEntity = null;
			if(file1!=null) {
				normalEntity = new Thumbnail();
				normalEntity.setSize(file1.getSize());
				normalEntity.setName(model.getLabelNormalText());
				normalEntity.setContentType(file1.getContentType());
				normalEntity.setUploadDate(new Date(System.currentTimeMillis()));
				normalEntity.setContent(new Binary(file1.getBytes()));
			}
			if(file2!=null) {
				activateEntity = new Thumbnail();
				activateEntity.setSize(file2.getSize());
				activateEntity.setName(model.getLabelNormalText());
				activateEntity.setContentType(file2.getContentType());
				activateEntity.setUploadDate(new Date(System.currentTimeMillis()));
				activateEntity.setContent(new Binary(file2.getBytes()));
			}
			if (StrKit.notBlank(model.getId())) {
				if (model.update()) {
					try {
						if (normalEntity != null) {
							normalEntity.setId(model.getId()+"_normal");
							thumbnailImpl.save(normalEntity);
						}
						if(activateEntity!=null) {
							activateEntity.setId(model.getId()+"_activate");
							thumbnailImpl.save(activateEntity);
						}
					} catch (Exception e) {
					}
					return R.ok(model);
				} else {
					return R.failed("更新失败！");
				}
			} else {
				model.setId(UuidUtil.getUUID());
				if (model.save()) {
					try {
						if (normalEntity != null) {
							normalEntity.setId(model.getId()+"_normal");
							thumbnailImpl.save(normalEntity);
						}
						if(activateEntity!=null) {
							activateEntity.setId(model.getId()+"_activate");
							thumbnailImpl.save(activateEntity);
						}
					} catch (Exception e) {
					}
					return R.ok(model);
				} else {
					return R.failed("保存失败！");
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@Tx
	@RequestMapping(value = "/deleteById", method = RequestMethod.GET)
	public R<String> deleteById(@RequestParam(name = "id") String id) throws Exception {
		try {
			SubStyleTemplatePoi model = SubStyleTemplatePoi.dao.findById(id);
			if (model == null)
				return R.failed("数据不存在，刷新后尝试!");
			if (model.delete()) {
				try {
					thumbnailImpl.deleteById(model.getId()+"_normal");
					thumbnailImpl.deleteById(model.getId()+"_activate");
				} catch (Exception e) {
				}
				return R.ok();
			}

			return R.failed("失败！");
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}
	
	@GetMapping(value = "/thumbnail/{id}", produces = { MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE })
	@ResponseBody
	public byte[] image(@PathVariable String id,@RequestParam(name = "type") String type) {
		byte[] data = null;
		Thumbnail thumbnail = thumbnailImpl.findById(id+"_"+type);
		if (thumbnail != null) {
			data = thumbnail.getContent().getData();
		}
		return data;
	}
	
}